package com.personal.gateway.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.personal.gateway.custom.customEnum.LoginTypeEnum;
import com.personal.gateway.constant.LoginUserInfoConstant;
import com.personal.gateway.dto.UserLoginDto;
import com.personal.gateway.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author BigHead
 * @date 2021/11/24 14:33
 * @description UserServiceImpl
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private LoginUserInfoConstant loginUserInfoConstant;

    @Override
    public String userLogin(UserLoginDto userLoginDto) {


        if (userLoginDto.checkParameter()) {
            return JSONObject.toJSONString(userLoginDto);
        }

        if (!loginUserInfoConstant.checkCaptcha(userLoginDto.getUid(), userLoginDto.getCaptcha())) {
            userLoginDto.setResponseCode(LoginTypeEnum.CAPTCHA_ERROR.getTypeValue());
            userLoginDto.setResponseMessage(LoginTypeEnum.CAPTCHA_ERROR.getTypeMessage());
            return JSONObject.toJSONString(userLoginDto);
        }

        if ("admin".equals(userLoginDto.getAccount())) {
            if ("admin".equals(userLoginDto.getPassword())) {
                String token = UUID.randomUUID().toString();
                loginUserInfoConstant.setUserToken(userLoginDto.getAccount(), token);
                userLoginDto.setResponseCode(LoginTypeEnum.OK.getTypeValue());
                userLoginDto.setResponseToken(token);
            } else {
                userLoginDto.setResponseCode(LoginTypeEnum.ACCOUNT_ERROR.getTypeValue());
                userLoginDto.setResponseMessage(LoginTypeEnum.ACCOUNT_ERROR.getTypeMessage());
            }
        } else {
            userLoginDto.setResponseCode(LoginTypeEnum.ACCOUNT_ERROR.getTypeValue());
            userLoginDto.setResponseMessage(LoginTypeEnum.ACCOUNT_ERROR.getTypeMessage());
        }
        return JSONObject.toJSONString(userLoginDto);
    }
}
